// 导入库
import 'dart:math';

// 每个应用都有一个顶层的 main 函数作为运行入口
// 没有指定返回类型的方法的返回类型会推导为 viod
void main() {
  print('Hello World!');

  // 变量：
  // 使用 var 关键字来定义变量，不用显示声明变量的类型（支持类型动态推断）

  var name = '坤坤';
  var age = 18;
  var hobby = ["唱", "跳", "rap"];
  var isMale = true;

  var obj = {name, age, hobby, isMale};

  print(name);
  print(age);
  print(hobby);
  print(isMale);

  var score = 80;
  if (score < 60) {
    print("不及格");
  } else if (score > 60 && score < 80) {
    print("及格");
  } else if (score >= 80 && score < 90) {
    print('良好');
  } else {
    print('优秀');
  }

  // for...in...遍历对象
  for (final key in obj) {
    print(key);
  }

  // 循环
  for (int i = 0; i < 10; i++) {
    print(i);
  }

  // while循环
  while (score < 100) {
    score++;
    print(score);
  }

  // 函数：建议为每个函数的参数以及返回值都指定类型
  int add(int a, int b) {
    return a + b;
  }

  print(add(24, 26));

  // 单行注释
  /* 多行注释 */

  var voyager = Spacecraft('Voyager I', DateTime(1977, 9, 5));
  voyager.describe();
}

class Spacecraft {
  String name;
  DateTime? launchDate;

  // 只读属性 getter
  int? get launchYear => launchDate?.year;

  Spacecraft(this.name, this.launchDate) {
    // 初始化代码
  }

  Spacecraft.unlaunched(String name) : this(name, null);

  void describe() {
    print('Spacecraft: $name');
    var launchDate = this.launchDate;
    if (launchDate != null) {
      int years = DateTime.now().difference(launchDate).inDays ~/ 365;
      print('Launched: $launchYear ($years years ago)');
    } else {
      print('Unlaunched');
    }
  }
}

// 枚举
enum PlanetType { terrestrial, gas, ice }

// 增强枚举
enum Planet {
  mercury(planetType: PlanetType.terrestrial, moons: 0, hasRings: false),
  venus(planetType: PlanetType.terrestrial, moons: 0, hasRings: false),
  // ···
  uranus(planetType: PlanetType.ice, moons: 27, hasRings: true),
  neptune(planetType: PlanetType.ice, moons: 14, hasRings: true);

  /// A constant generating constructor
  const Planet(
      {required this.planetType, required this.moons, required this.hasRings});

  /// All instance variables are final
  final PlanetType planetType;
  final int moons;
  final bool hasRings;

  /// Enhanced enums support getters and other methods
  bool get isGiant =>
      planetType == PlanetType.gas || planetType == PlanetType.ice;
}

// 扩展类
class Orbiter extends Spacecraft {
  double altitude;

  Orbiter(super.name, DateTime super.launchDate, this.altitude);
}

// Mixins
// Mixins是一种在多个类层次结构中重用代码的方法
mixin Piloted {
  int astronauts = 1;

  void describeCrew() {
    print('Number of astronauts: $astronauts');
  }
}

// class PilotedCraft extends Spacecraft with Piloted {}

// 接口和抽象类
// class MockSpaceship implements Spacecraft {}

// 抽象类
abstract class Describable {
  void describe();

  void describeWithEmphasis() {
    print('=========');
    describe();
    print('=========');
  }
}

// 异步
// 使用 async 和 await 关键字可以避免回调地狱
const oneSecond = Duration(seconds: 1);

Future<void> printWithDelay(String message) async {
  await Future.delayed(oneSecond);
  print(message);
}


// 异常
// if (astronauts == 0) {
//   throw StateError('No astronauts.');
// }
